package edu.uit.se.laps.web.view;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import edu.uit.se.laps.web.controller.LoaiSanPhamManager;
import edu.uit.se.laps.web.controller.SanPhamManager;
import edu.uit.se.laps.web.controller.ShoppingCart;
import edu.uit.se.laps.web.model.CTDonHang;
import edu.uit.se.laps.web.model.LoaiSanPham;
import edu.uit.se.laps.web.model.SanPham;

public class SanPhamAction extends ActionSupport  implements ServletRequestAware, ServletResponseAware {

	/**
	 * Properties
	 */
	private static final long serialVersionUID = 1199884620207785177L;
	private HttpServletRequest request;
    private HttpServletResponse response;
	
	public int start = 0;
	public int stop = 8;
	public int stopNu = 8;
	public int stopNam = 8;
	public int stopTre = 8;
	private SanPham sp;
	private List<SanPham> spList;
	private int idsp;				//bien toan cuc, xu li ben jsp
	private int soLuong;			//bien toan cuc, xu li ben jsp
	
	private SanPhamManager spManager;
	
	//
	//private CTDonHang cTDH;
	private CTDonHang ctdh;
	//private CTDonHangManager cTDHManager;
	private List<CTDonHang> cTDHList;
	
	private ShoppingCart cart;
	private List<SanPham> carts;
	
	
	private LoaiSanPham loaiSp;
	private LoaiSanPhamManager loaiSpManager;
	
	
	public SanPhamAction(){
		spManager = new SanPhamManager();
		
		//cTDHManager = new CTDonHangManager();
		loaiSpManager = new LoaiSanPhamManager();
		
		cart = new ShoppingCart();
		
		carts = new ArrayList<>();
		cTDHList = new ArrayList<>();
	}
	
	//chay dc chua?
	//de chay lai
	/*
	 *	Method 
	 */
	
	//execute la phuong thuc chinh, bat buoc phai co
	public String execute(){
		this.spList = spManager.list();
		System.out.println("execute called");
		//no lay dc danh sach ma
		
		return SUCCESS;
	}
	
	@Override
	public void validate(){
//		if (this.getSp().getIdLoaiSP() <= 0) {
//			addFieldError("sp.idLoaiSP", "Loại sản phẩm không được bằng hoặc nhỏ 0");
//		}
//		if (this.getSp().getTenSP().length() == 0) {
//			addFieldError("sp.tenSP", "Tên sản phẩm không được để trống");
//		}
	}
	
	public String addSP(){
		String idloaisp = request.getParameter("idLoaiSP");
		//String idcl = request.getParameter("idCL");
		String tensp = request.getParameter("tenSP");
		String mota = request.getParameter("moTa");
		String dacdiem = request.getParameter("dacDiem");
		String dieukien = request.getParameter("dieuKien");
		String hinhanh = request.getParameter("srcImg");
		String hinhanhct = request.getParameter("srcImgCT");
		String gia = request.getParameter("gia");
		String soluongton = request.getParameter("soLuongTon");
		String ghichu = request.getParameter("ghiChu");
		
		LoaiSanPham loaiSP = new LoaiSanPham();
		loaiSP = loaiSpManager.getLoaiSP(idloaisp);
		
		int idLoaiSP = loaiSP.getIdLoaiSP();
		//int idCL = Integer.parseInt(idcl);
		float giaSP = Integer.parseInt(gia);
		int soLuong = Integer.parseInt(soluongton);
		
		SanPham sanpham = new SanPham();
		//sanpham.setIdCL(idCL);
		sanpham.setIdLoaiSP(idLoaiSP);
		sanpham.setTenSP(tensp);
		sanpham.setMoTa(mota);
		sanpham.setDacDiem(dacdiem);
		sanpham.setDieuKien(dieukien);
		sanpham.setSrcImg(hinhanh);
		sanpham.setSrcImgCT(hinhanhct);
		sanpham.setGia(giaSP);
		sanpham.setSoLuongTon(soLuong);
		sanpham.setGhiChu(ghichu);
		
		
		System.out.println(getSp());
		try {
			spManager.addSanPham(sanpham);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.spList = spManager.list();
		return SUCCESS;
	}
	
	public String deleteSP(){
		String idsp = request.getParameter("idSP");
		int id = Integer.parseInt(idsp);
		sp = spManager.deleteSanPham(id);
		return SUCCESS;
	}
	
	public String updateSP() {
		String idsp = request.getParameter("idSP");
		String idloaisp = request.getParameter("idLoaiSP");
		//String idcl = request.getParameter("idCL");
		String tensp = request.getParameter("tenSP");
		String mota = request.getParameter("moTa");
		String dacdiem = request.getParameter("dacDiem");
		String dieukien = request.getParameter("dieuKien");
		String hinhanh = request.getParameter("srcImg");
		String hinhanhct = request.getParameter("srcImgCT");
		String gia = request.getParameter("gia");
		String soluongton = request.getParameter("soLuongTon");
		String ghichu = request.getParameter("ghiChu");
		
		LoaiSanPham loaiSP = new LoaiSanPham();
		loaiSP = loaiSpManager.getLoaiSP(idloaisp);
		
		int idsptemp = Integer.parseInt(idsp);
		int idLoaiSP = loaiSP.getIdLoaiSP();
		//int idCL = Integer.parseInt(idcl);
		float giaSP = Float.parseFloat(gia);
		int soLuong = Integer.parseInt(soluongton);
		
		SanPham sanpham = new SanPham();
		sanpham.setIdSP(idsptemp);
		//sanpham.setIdCL(idCL);
		sanpham.setIdLoaiSP(idLoaiSP);
		sanpham.setTenSP(tensp);
		sanpham.setMoTa(mota);
		sanpham.setDacDiem(dacdiem);
		sanpham.setDieuKien(dieukien);
		sanpham.setSrcImg(hinhanh);
		sanpham.setSrcImgCT(hinhanhct);
		sanpham.setGia(giaSP);
		sanpham.setSoLuongTon(soLuong);
		sanpham.setGhiChu(ghichu);
		
		
		//System.out.println(getSp());
		try {
			spManager.updateSanPham(sanpham);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.spList = spManager.list();
		return SUCCESS;
	}
	
	public String showupdate(){
		//Lay id sp tu request
		String idSP_str = request.getParameter("idSP");
		
		int id = Integer.parseInt(idSP_str);
		SanPham sp = spManager.getSanPham(id);
		
		HttpSession session = request.getSession();		
		//get the cart
		ShoppingCart shopCart = (ShoppingCart) session.getAttribute("ShoppingCart");
		shopCart.setSpTemp(sp);		
		session.setAttribute("ShoppingCart", shopCart);
		
		return SUCCESS;
	}
	
	public String addToShoppingCart(){
		//lay sp tu danh sach sp dua vao id
		String idspstr = request.getParameter("idspstr");
		int id = Integer.parseInt(idspstr);
		String soLuongMua = request.getParameter("soLuongMua");
		int slMua = Integer.parseInt(soLuongMua);
		
		
		int i = soLuong;
		try {
			sp = spManager.getSanPham(id);
			if (sp == null){
				System.out.println("sp null");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		CTDonHang ctDH = new CTDonHang();
		//ctDH = new CTDonHang(0, idsp, sp.getGia(), soLuong, sp.getGia() * soLuong);
		ctDH.setIdDH(0);
		ctDH.setIdSP(id);
		ctDH.setDonGia(sp.getGia());
		ctDH.setSoLuong(slMua);
		ctDH.setThanhTien(ctDH.getDonGia() * ctDH.getSoLuong());
		//cTDHManager.addCTDonHang(ctDH);
		
		
		HttpSession session = request.getSession();
		
		//get the cart
		ShoppingCart shopCart = (ShoppingCart) session.getAttribute("ShoppingCart");
		//shopCart.addSPList(sp);
		shopCart.addCTDHList(ctDH);
		
		session.setAttribute("ShoppingCart", shopCart);
		//carts = shopCart.getSPList();
		cTDHList = shopCart.getcTDHList();
		
		return SUCCESS;
	}
	
	
	public String addToShopCart(){
		//String idSpStr = request.getParameter("idSP");
		//String soLuongStr = request.getParameter("soLuong"); 
		//int idsp = Integer.parseInt(idSpStr);
//		int soLuong = Integer.parseInt(soLuongStr);
		
		//lay ctsp tu danh sach sp dua vao id
		//SanPham sanPham;
		int i = getSoLuong();
		try {
			sp = spManager.getSanPham(idsp);
			if (sp == null){
				System.out.println("sp null" + i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		
//		cTDH = new CTDonHang();
//		cTDH.setIdDH(0);
//		cTDH.setIdSP(idsp);
//		cTDH.setDonGia(sp.getGia());
//		cTDH.setSoLuong(i);
//		cTDH.setThanhTien(cTDH.getDonGia() * cTDH.getSoLuong());
//		cTDHManager.addCTDonHang(cTDH);
		
		//cTDHManager.addCTDonHang(sp, soLuong);
		//HttpSession session = request.getSession();
		
		//get the cart
		//cart.addItem(sp);
		cart.addSPList(sp);
		//carts = cart.getSPList();
		carts.add(sp);// = cart.getSPList();
		//ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart");
		//cart.addItem(cTDH);
		
		//session.setAttribute("ShoppingCart", cart);
		//this.cTDHList = cTDHManager.list();
		
		return SUCCESS;
	}
	 
	public String showSpNam(){
		this.spList = spManager.listSp(1);
		System.out.println("show sp nam called");
		
		return SUCCESS;
	}
	public String showSpNu(){
		this.spList = spManager.listSp(2);
		System.out.println("show sp nu called");
		
		return SUCCESS;
	}
	public String showSpTre(){
		this.spList = spManager.listSp(3);
		System.out.println("show sp Tre called");
		
		return SUCCESS;
	}
	
	
	
	/**
	 * Set - Get Properties
	 */
	public SanPham getSp() {
		return sp;
	}
	public void setSp(SanPham sp) {
		this.sp = sp;
	}
	public List<SanPham> getSpList() {
		return spList;
	}
	public void setSpList(List<SanPham> spList) {
		this.spList = spList;
	}
	public int getIdsp() {
		return idsp;
	}
	public void setIdsp(int idsp) {
		this.idsp = idsp;
	}

	

	public int getSoLuong() {
		return soLuong;
	}

	public void setSoLuong(int soLuong) {
		this.soLuong = soLuong;
	}

	public ShoppingCart getCart() {
		return cart;
	}

	public void setCart(ShoppingCart cart) {
		this.cart = cart;
	}

	public List<SanPham> getCarts() {
		return carts;
	}

	public void setCarts(List<SanPham> carts) {
		this.carts = carts;
	}

	
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

	public CTDonHang getCtdh() {
		return ctdh;
	}

	public void setCtdh(CTDonHang ctdh) {
		this.ctdh = ctdh;
	}

	public List<CTDonHang> getcTDHList() {
		return cTDHList;
	}

	public void setcTDHList(List<CTDonHang> cTDHList) {
		this.cTDHList = cTDHList;
	}

}
